package com.manager.plat.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.manager.plat.dao.IConfigBusinessDao;
import com.manager.plat.util.BaseUtil;
import com.manager.plat.vo.ConfigBusinessVo;
/**
 * 网关信息数据实现层
 * @author liugeng
 *
 */
public class ConfigBusinessDaoImpl extends JdbcDaoSupport implements IConfigBusinessDao {
	private static Logger logger = Logger.getLogger(ConfigBusinessDaoImpl.class);
	
	/**获得所有网管配置信息列表*/
	@SuppressWarnings("unchecked")
	@Override
	public List<ConfigBusinessVo> getConfigBusinessList() {
		String sql = "select id, sp_code, day_limit, month_limit, one_shot_limit, forbidden_province, forbidden_city, fee_type, fee_code, start_time, end_time, add_date from tb_ivr_config_business where status = 1";
		logger.info(String.format("获得所有网关配置信息列表[%s]", sql));
		List<ConfigBusinessVo> configBusinessVoList = this.getJdbcTemplate().query(sql, rowMapper);
		
		if(configBusinessVoList != null && configBusinessVoList.size() > 0){
			return configBusinessVoList;
		}
		return null;
	}
	/**根据Id获得网关配置信息*/
	@SuppressWarnings("unchecked")
	@Override
	public ConfigBusinessVo getConfigBusinessById(int id) {
		String sql = "select id, sp_code, day_limit, month_limit, one_shot_limit, forbidden_province, forbidden_city, fee_type, fee_code, start_time, end_time, add_date from tb_ivr_config_business where id = ? and status = 1";
		Object[] param = new Object[]{id};
		logger.info(String.format("根据Id获得网关配置信息[%s]", BaseUtil.logSQL(sql, param)));
		List<ConfigBusinessVo> configBusinessVoList = this.getJdbcTemplate().query(sql, param, rowMapper);
		
		if(configBusinessVoList != null && configBusinessVoList.size() > 0){
			return configBusinessVoList.get(0);
		}
		return null;
	}
	private ParameterizedRowMapper<ConfigBusinessVo> rowMapper = new ParameterizedRowMapper<ConfigBusinessVo>() {
		@Override
		public ConfigBusinessVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			ConfigBusinessVo configBusinessVo = new ConfigBusinessVo();
			configBusinessVo.setId(rs.getInt("id"));
			configBusinessVo.setSp_code(rs.getString("sp_code"));
			configBusinessVo.setDay_limit(rs.getInt("day_limit"));
			configBusinessVo.setMonth_limit(rs.getInt("month_limit"));
			configBusinessVo.setOne_shot_limit(rs.getInt("one_shot_limit"));
			configBusinessVo.setForbidden_province(rs.getString("forbidden_province"));
			configBusinessVo.setForbidden_city(rs.getString("forbidden_city"));
			configBusinessVo.setFee_type(rs.getInt("fee_type"));
			configBusinessVo.setFee_code(rs.getInt("fee_code"));
			configBusinessVo.setStart_time(rs.getString("start_time"));
			configBusinessVo.setEnd_time(rs.getString("end_time"));
			configBusinessVo.setAdd_date(rs.getTimestamp("add_date"));
			return configBusinessVo;
		}
	};
	/**新增网关配置信息*/
	@Override
	public int insertConfigBusiness(ConfigBusinessVo configBusinessVo) {
		String sql = "insert into tb_ivr_config_business (sp_code, day_limit, month_limit, one_shot_limit, forbidden_province, forbidden_city, fee_type, fee_code, start_time, end_time) values(?,?,?,?,?,?,?,?,?,?)";
		Object[] param = new Object[]{configBusinessVo.getSp_code(), configBusinessVo.getDay_limit(),
									configBusinessVo.getMonth_limit(), configBusinessVo.getOne_shot_limit(),
									configBusinessVo.getForbidden_province(), configBusinessVo.getForbidden_city(),
									configBusinessVo.getFee_type(), configBusinessVo.getFee_code(),
									configBusinessVo.getStart_time(), configBusinessVo.getEnd_time()};
		logger.info(String.format("保存网关配置信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		
		return count;
	}
	/**更新网关配置信息*/
	@Override
	public int updateConfigBusiness(ConfigBusinessVo configBusinessVo) {
		String sql = "update tb_ivr_config_business set sp_code = ?, day_limit = ?, month_limit = ?, one_shot_limit = ?, forbidden_province = ?, forbidden_city = ?, fee_type = ?, fee_code = ?, start_time = ?, end_time = ? where id = ?";
		Object[] param = new Object[]{configBusinessVo.getSp_code(), configBusinessVo.getDay_limit(),
									configBusinessVo.getMonth_limit(), configBusinessVo.getOne_shot_limit(),
									configBusinessVo.getForbidden_province(), configBusinessVo.getForbidden_city(),
									configBusinessVo.getFee_type(), configBusinessVo.getFee_code(),
									configBusinessVo.getStart_time(), configBusinessVo.getEnd_time(),
									configBusinessVo.getId()};
		logger.info(String.format("更新网关配置信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**删除网关配置信息*/
	@Override
	public int deleteConfigBusiness(int id) {
		String sql = "update tb_ivr_config_business set status = 0 where id = ?";
		Object[] param = new Object[]{id};
		logger.info(String.format("删除网关配置信息[%s]", BaseUtil.logSQL(sql, param)));

		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**根据渠道登录信息获得渠道对应的网关配置信息*/
	@SuppressWarnings("unchecked")
	@Override
	public List<ConfigBusinessVo> getPartnerConfigBusinessList(int userId) {
		String sql = "select tcb.id, tcb.sp_code " +
					 "from  ivr_router ir " +
					 "inner join plat_lower_partner plp on plp.id = ir.lower_id and plp.status = 1 " +
					 "inner join tb_ivr_config_business tcb on tcb.id = ir.gateway_id and tcb.status = 1 " +
					 "where plp.userId = ? and ir.status = 1";
		Object[] param = new Object[]{userId};
		logger.info(String.format("根据渠道登录信息获得渠道对应的网关配置信息[%s]", BaseUtil.logSQL(sql, param)));
		
		List<ConfigBusinessVo> ivrGatewayVoList = this.getJdbcTemplate().query(sql, param, tempMapper);
		if(ivrGatewayVoList != null && ivrGatewayVoList.size() > 0){
			return ivrGatewayVoList;
		}
		return null;
	}
	private ParameterizedRowMapper<ConfigBusinessVo> tempMapper = new ParameterizedRowMapper<ConfigBusinessVo>() {
		@Override
		public ConfigBusinessVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			ConfigBusinessVo ivrGatewayVo = new ConfigBusinessVo();
			ivrGatewayVo.setId(rs.getInt("id"));
			ivrGatewayVo.setSp_code(rs.getString("sp_code"));
			return ivrGatewayVo;
		}
	};
}
